package Test_Server;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ServerBoard extends JFrame {

    private JTextArea messagesArea;
    private JButton sendButton;
    private JTextField message;
    private JButton startServer;
    private TCPServer mServer;

    public ServerBoard() {
        super("ServerBoard");
        JPanel panelFields = new JPanel();
        panelFields.setLayout(new BoxLayout(panelFields, BoxLayout.X_AXIS));
        JPanel panelFields2 = new JPanel();
        panelFields2.setLayout(new BoxLayout(panelFields2, BoxLayout.X_AXIS));
//here we will have the text messages screen
        messagesArea = new JTextArea();
        messagesArea.setColumns(30);
        messagesArea.setRows(10);
        messagesArea.setEditable(false);
        sendButton = new JButton("Send");
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
// get the message from the text view
                String messageText = message.getText();
// add message to the message area
                messagesArea.append("\n" + messageText);
// send the message to the client
                mServer.sendMessage(messageText);
// clear text
                message.setText("");
            }
        });
        startServer = new JButton("Start");
        startServer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
// disable the start button
                startServer.setEnabled(false);
//creates the object OnMessageReceived asked by the TCPServer constructor
                mServer = new TCPServer(new TCPServer.OnMessageReceived() {
                    @Override
//this method declared in the interface from TCPServer class is implemented here
//this method is actually a callback method, because it will run every time when it will be called from
//TCPServer class (at while)
                    public void messageReceived(String message) {
                        if (message.startsWith("[")) {
                            try {
                                JSONObject jsonObject = (JSONObject) new JSONParser().parse(message);
                                System.out.println(jsonObject.keySet());
                                System.out.println(JSONObject.toString(message, this));

                            } catch (ParseException ex) {
                                Logger.getLogger(ServerBoard.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            messagesArea.append("\n " + message + " " + message.getClass());
                            mServer.sendMessage(
                            
                                "{\"Item\": [ {"
                                    + "\"EAN\": 50096381,"
                                    + "\"Item_Name\": \"DOVE ROLL-ON FRESH\","
                                    + "\"Item_Price\": 34.95,"
                                    + "\"Design\": 061417,"
                                    + "\"Colli_Size\": 6,"
                                    + "\"Stock_Price\": 29.95,"
                                    + "\"Defult_Label_Line_1\": \"DOVE ROLL-ON FRESH\","
                                    + "\"Defult_Label_Line_2\": \"50 ML\","
                                    + "\"Custom_Label_Line_1\": \"\","
                                    + "\"Custom_Label_Line_2\": \"\","
                                    + "\"Label_Size\": 1,"
                                    + "\"Label_Amount\": 1,"
                                    + "\"Custom_Label_Amount\": 1,"
                                    + "\"Sign\": [ {"
                                        + "\"Line_Nr\": 0,"
                                        + "\"Line_Text\": \"DOVE ROLL-ON\""
                                    +"},{"
                                        + "\"Line_Nr\": 1,"
                                        + "\"Line_Text\": \"FRIT VALG\""                                    
                                    + " }],"
                                    + "\"Offer\": [ {"
                                        + "\"Offer_Name\": \"Dove\","
                                        + "\"Description\": \"Dove 15,-\","
                                        + "\"Start_Date\": 041112,"
                                        + "\"End_Date\": 101112"
                                    +"},{"
                                        + "\"Offer_Name\": \"Dove\","
                                        + "\"Description\": \"Dove 15,-\","
                                        + "\"Start_Date\": 021212,"
                                        + "\"End_Date\": 081212"
                                    + " }]"     
                                + " }]"
                                + "}");
                        }
                    }
                });
                mServer.start ();
        }
        
    }
    );
//the box where the user enters the text (EditText is called in Android)
        message  = new JTextField();

    message.setSize (

    200, 20);
//add the buttons and the text fields to the panel
        panelFields.add (messagesArea);

    panelFields.add (startServer);

    panelFields2.add (message);

    panelFields2.add (sendButton);

    getContentPane()

    .add(panelFields);
        getContentPane()

    .add(panelFields2);
        getContentPane()

    .setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        setSize(

    300, 170);
        setVisible(

true);
    }
}